<?php
class UserService{
var $dbutil;

		function __construct($dbutil){
			$this->dbutil = $dbutil;
		}
		
		function LoginGetLatestBets($sessionid){
			$sql = "select b.userid,b.username,b.email,a.bet_time,a.bet_vmoney,b.user_photo,c.event_id, c.team_mian_name,c.team_sec_name,a.odds_name,a.bet_odd,d.id as watchid 
						from 
							(select user_name,event_id,bet_time,bet_vmoney,odds_name,bet_odd from ibo_bet order by bet_time desc limit 15) a,
							users b,
							ibo_event c ,ibo_watch_user d
						where 
							a.user_name=b.user_email and a.event_id=c.event_id and d.user_id=".$sessionid." and d.watch_user_id="."b.user_id";
			
			$sql2 = "select tt.*,d.id as watchid from (select b.userid,b.username,b.email,a.bet_time,a.bet_vmoney,b.user_photo,c.event_id,c.team_mian_name,c.team_sec_name,a.odds_name,a.bet_odd
						from 
							(select user_name,event_id,bet_time,bet_vmoney,odds_name,bet_odd from ibo_bet order by bet_time desc limit 15) a,
							users b,
							ibo_event c 
						where 
							a.user_name=b.email and a.event_id=c.event_id) tt left join ibo_watch_user d on d.user_id=".$sessionid." and d.watch_user_id=tt.userid";
			return $this->dbutil->get_results($sql2);
		}
		
	function noLoginGetLatestBets(){
			$sql = "select b.userid,b.username,b.email,a.bet_time,a.bet_vmoney,b.user_photo,c.event_id,c.team_mian_name,c.team_sec_name,a.odds_name,a.bet_odd,NULL as watchid 
						from 
							(select user_name,event_id,bet_time,bet_vmoney,odds_name,bet_odd from ibo_bet order by bet_time desc limit 15) a,
							users b,
							ibo_event c 
						where 
							a.user_name=b.email and a.event_id=c.event_id;";
			return $this->dbutil->get_results($sql);
		}
		
	function userFollow($fans,$superStar){
			$data = array("user_id"=>$fans,
					  	  "watch_user_id"=> $superStar,
					      "watch_time"=>time());
			return $this->dbutil->insert('ibo_watch_user', $data);
		}
		
	function userUnFollow($fans,$superStar){
			$this->dbutil->query("delete from ibo_watch_user where user_id='" . $fans . "' and watch_user_id='".$superStar."';");
		}
		
	//��ȡ�ж����˹�ע����û�
	function getUersFans($sperStarId){
		$sql = "select count(*) as total from ibo_watch_user where user_id = '" .$sperStarId ."';";
		return $this->dbutil->get_results($sql);
	} 
	
	//��ȡ����û���ע�˶�����
	function getUersSuperStar($userId){
		$sql = "select count(*) as total from ibo_watch_user where watch_user_id = '" .$userId ."';";
		return $this->dbutil->get_results($sql);
	}
	
/**
	 * 检查用户邮箱是否唯一
	 * Enter description here ...
	 * @param unknown_type $email
	 */
	function checkEmailUnique($email){
		$email = $this->dbutil->get_var("select user_email from ibo_user where user_email='".$email."'");
		if($email){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 
	 * 登录成功
	 * @param unknown_type $email
	 * @param unknown_type $pass
	 */
	function addUser($email, $pass, $name, $photo, $weiboflag = null){
		$user = array("user_email"=>$email,
					  "user_passwd"=> encodePassword($pass),
					  "user_exp"=> 0,
					  "user_vmoney"=>100,
					  "user_regdate"=> date("c"),
					  "user_lastdate"=> date("c"),
					  "user_name"=> $name, 
					  "user_photo" => $photo,
					  "weibo_flag" => $weiboflag);
		$this->dbutil->insert("ibo_user", $user);
		
		$user = $this->dbutil->get_row("select * from ibo_user where user_email='".$email."'");
		$_SESSION['user'] = $user;
	}
	
	function getUser($email){
		$user = $this->dbutil->get_row("select * from ibo_user where user_email='".$email."'");
		return $user;
	}
	
	function getUserById($id){
		$user = $this->dbutil->get_row("select * from users where userid='".$id."'");
		return $user;
	}
	
	function updateUsername($userKey,$username){
		return $this->dbutil->update("users", array("username"=> $username), array("userid"=>$userKey) );
	}
	
	function updatePassword($userKey,$password){
		return $this->dbutil->update("users", array("password"=> $password), array("userid"=>$userKey) );
	}
	
	function updatePhoto($userKey,$photoPath){
		$res = $this->dbutil->update("users", array("user_photo"=> $photoPath), array("userid"=>$userKey) );
		return $res;
	}
	
	function updateUserMoney($user){
		$this->dbutil->update("users", array("user_vmoney"=> $user->user_vmoney), array("userid"=>$user->userid) );
	}
	
	function updateUserMessage($user, $id){
		$this->dbutil->update("users", $user, array("userid"=>$id));
	}
	
	function getLastThreeBetWathedUser($userid){
		$sql = "select a.* from ("
				."select u.user_id, u.user_name,u.user_email,u.user_exp, u.user_vmoney, u.user_photo, b.event_id, b.bet_vmoney, b.odds_name, b.bet_time , e.team_mian_name, e.team_sec_name "
				."from ibo_watch_user i, ibo_user u, ibo_bet b, ibo_event e "
				."where e.event_id = b.event_id and  u.user_email = b.user_name and u.user_id = i.watch_user_id and i.user_id = $userid "
				.") a "
				."where 3 > ( "
				."select count(*) from ibo_bet be where a.user_email = be.user_name and be.bet_time > a.bet_time )";

		return $this->dbutil->get_results($sql);
	}
	
function checkWatchUser($userid,$watchUserid){
		
		$sql = " select * from ibo_watch_user where user_id='$userid' and watch_user_id='$watchUserid'";
		
		$result = $this->dbutil->get_results($sql);
		
		if($result == null){
			return false;	
		}else{
			return true;
		}
	}
	
	function watchUser($userid,$watchUserid){
// 		$data = array("user_id"=>$userid,
// 			"watch_user_id"=> $watchUserid,
// 		    "watch_time"=>time());
// 		return $this->dbutil->insert('ibo_watch_user', $data);
		$sql = "insert into ibo_watch_user(user_id , watch_user_id ,watch_time) values('$userid','$watchUserid', sysdate())";
		return $this->dbutil->query($sql);
	}
	
	function unWatchUser($userid,$watchUserid){
		$sql = "delete from ibo_watch_user where user_id = '$userid' and watch_user_id = '$watchUserid' ";
		return $this->dbutil->query($sql);
	}
	
	function watchUserList($userid){
		$sql = "select user.user_photo as photo,user.user_id as userid ,count(watch.user_id) as wcount ".
			"from ibo_user user,ibo_watch_user watch ".
			"where user.user_id = watch.user_id ".
			//" and  user.user_id != '$userid' ".
			"group by user.user_photo,user.user_id order by wcount desc limit 6 ";
		return $this->dbutil->get_results($sql);
	}
}
?>